//
//  CustomerDetailVC.m
//  New_Runway
//
//  Created by mack on 2018/2/9.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CustomerDetailVC.h"
#import "CustomerInfoModel.h"
#import "CustomerMessageCell.h"
#import "CustomerOtherCell.h"
#import "LoveCarChartCell.h"
#import "LoveCarPriceCell.h"
#import "LoveCarAgeCell.h"
#import "LoveSeriesCell.h"
#import "TagsFrame.h"


@interface CustomerDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSMutableArray *ageList;
@property (nonatomic, strong) NSMutableArray *ageNames;

@property (nonatomic, strong) NSMutableArray *priceAllArr;
@property (nonatomic, strong) NSMutableArray *priceNames;

@property (nonatomic, strong) NSMutableArray *seriesList;
@property (nonatomic, strong) NSMutableArray *seriesNames;

@property (nonatomic, assign) CGFloat ageTotal;
@property (nonatomic, assign) CGFloat priceTotal;
@property (nonatomic, assign) CGFloat seriesTotal;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) NSMutableArray *tagsFrames;

@end

@implementation CustomerDetailVC
{
    CustomerInfoModel *_infoModel;
    
}

-(NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}
-(NSMutableArray *)ageList{
    if (!_ageList) {
        _ageList = [[NSMutableArray alloc] init];
    }
    return _ageList;
}

-(NSMutableArray *)ageNames{
    if (!_ageNames) {
        _ageNames = [[NSMutableArray alloc] init];
    }
    return _ageNames;
}
-(NSMutableArray *)priceAllArr{
    if (!_priceAllArr) {
        _priceAllArr = [[NSMutableArray alloc] init];
    }
    return _priceAllArr;
}

-(NSMutableArray *)priceNames{
    if (!_priceNames) {
        _priceNames = [[NSMutableArray alloc] init];
    }
    return _priceNames;
}
-(NSMutableArray *)seriesList{
    if (!_seriesList) {
        _seriesList = [[NSMutableArray alloc] init];
    }
    return _seriesList;
}
-(NSMutableArray *)seriesNames{
    if (!_seriesNames) {
        _seriesNames = [[NSMutableArray alloc] init];
    }
    return _seriesNames;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"客户详情";
    self.view.backgroundColor = kColorWhite();
    [self loadCustomerMessage];
    [self initTableview];
    self.number = 1;
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"CustomerMessageCell" bundle:nil] forCellReuseIdentifier:@"CustomerMessageCell"];
    [self.tableview registerClass:[CustomerOtherCell class] forCellReuseIdentifier:@"CustomerOtherCell"];
    [self.tableview registerClass:[LoveCarChartCell class] forCellReuseIdentifier:@"LoveCarChartCell"];
    [self.tableview registerClass:[LoveCarPriceCell class] forCellReuseIdentifier:@"LoveCarPriceCell"];
    [self.tableview registerClass:[LoveCarAgeCell class] forCellReuseIdentifier:@"LoveCarAgeCell"];
    [self.tableview registerClass:[LoveSeriesCell class] forCellReuseIdentifier:@"LoveSeriesCell"];


    
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_offset(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
            return 120;
       
    }else if (indexPath.section == 1){
        return [_tagsFrames[0] tagsHeight];

    }
    return 300;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
            CustomerMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomerMessageCell"];
            cell.infoModel = _infoModel;
            cell.customerId = self.customerId;
            cell.nav = self.navigationController;
            
            return cell;
        
        
        
    }else if (indexPath.section == 1){
        
        CustomerOtherCell *cell = [CustomerOtherCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.tagsFrame = _tagsFrames[0];
        return cell;
        
      
        
    }else if (indexPath.section == 2){
            LoveCarChartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoveCarChartCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.infoModel = _infoModel;
            
            return cell;
        
        
    }else if (indexPath.section == 3){
        LoveCarPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoveCarPriceCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.priceAllArray = self.priceAllArr;
        cell.priceTotal = self.priceTotal;
        cell.priceNames = self.priceNames;
        return cell;
        
       
    }else if (indexPath.section == 4){
        LoveCarAgeCell*cell = [tableView dequeueReusableCellWithIdentifier:@"LoveCarAgeCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.ageTotal = self.ageTotal;
        cell.ageNames = self.ageNames;
        cell.ListArray = self.ageList;
        return cell;
        
    }
    
    LoveSeriesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoveSeriesCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.seriesTotal = self.seriesTotal;
    cell.seriesNames = self.seriesNames;
    cell.ListArray = self.seriesList;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    NSArray *array = @[@"",@"偏爱车辆标签",@"偏爱车辆",@"偏爱车车辆价格",@"偏爱车车辆年龄",@"偏爱车系"];
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = array[section];
    titleL.textColor = UIColorHex(0x323232);
    titleL.font = [UIFont systemFontOfSize:14];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).mas_offset(16);
        make.centerY.equalTo(view);
    }];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.0001;
    }
    return 30;
}
-(void)loadCustomerMessage{
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,customerInfoApi];
    [SVProgressHUD show];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"customerShopId":self.customerId} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        _infoModel = [CustomerInfoModel mj_objectWithKeyValues:response];
        [SVProgressHUD dismiss];
        if (_infoModel.code == 1) {
            for (NSDictionary *dic in _infoModel.result.intentionInfo.ageList) {
                Agelist *list = [Agelist mj_objectWithKeyValues:dic];
                self.ageTotal += list.storageNum;
                NSMutableDictionary *ageDic = [[NSMutableDictionary alloc] init];
                [ageDic setValue:list.carAgeRemark forKey:@"name"];
                [ageDic setValue:@(list.storageNum) forKey:@"value"];
                [self.ageList addObject:ageDic];
                [self.ageNames addObject:list.carAgeRemark];
                [self.titleArray addObject:list.carAgeRemark];
            }
            for (NSDictionary *dic  in _infoModel.result.intentionInfo.priceList) {
                Pricelist *list = [Pricelist mj_objectWithKeyValues:dic];
                self.priceTotal += list.storageNum;
                NSMutableDictionary *priceDic = [[NSMutableDictionary alloc] init];
                [priceDic setValue:list.priceRemark forKey:@"name"];
                [priceDic setValue:@(list.storageNum) forKey:@"value"];
                [self.priceAllArr addObject:priceDic];
                [self.priceNames addObject:list.priceRemark];
                
                [self.titleArray addObject:list.priceRemark];
            }
            
            for (NSDictionary *dic in _infoModel.result.intentionInfo.seriesList) {
                Serieslist *list = [Serieslist mj_objectWithKeyValues:dic];
                self.seriesTotal += list.storageNum;
                NSMutableDictionary *serieseDic = [[NSMutableDictionary alloc] init];
                [serieseDic setValue:list.seriesName forKey:@"name"];
                [serieseDic setValue:@(list.storageNum) forKey:@"value"];
                [self.seriesList addObject:serieseDic];
                [self.seriesNames addObject:list.seriesName];
                [self.titleArray addObject:list.seriesName];
                
                
            }
            _tagsFrames = [NSMutableArray array];
            TagsFrame *frame = [[TagsFrame alloc] init];
            frame.tagsMinPadding  = 5;
            frame.tagsMargin      = 20;
            frame.tagsLineSpacing = 10;
            frame.tagsArray = self.titleArray;
            [_tagsFrames addObject:frame];

            [self.tableview reloadData];
        } else {
            [ShowMessage showHintMessage:_infoModel.msg duration:1 withRemindId:remindWarning];
        }
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

        [SVProgressHUD dismiss];

    }];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
